local sk__chuyuan = fk.CreateSkill {

  name = "sk__chuyuan",

  tags = {},

}



sk__chuyuan:addEffect(fk.CardUseFinished, {
  name = "sk__chuyuan",
  anim_type = "drawcard",
  derived_piles = "sk__godcaopi_chu",
  events = { fk.CardUseFinished, fk.DrawNCards },
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__chuyuan.name) and player:usedSkillTimes("sk__dengji", Player.HistoryGame) == 0 then
      if event == fk.CardUseFinished then
        return data.card.trueName == "slash" or data.card.trueName == "jink"
      else
        return target == player and table.find(player:getPile("sk__godcaopi_chu"), function(id)
          return Fk:getCardById(id).color == Card.Black
        end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.CardUseFinished then
      return player.room:askForSkillInvoke(player, sk__chuyuan.name)
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      player:drawCards(2, sk__chuyuan.name)
      if player.dead or player:isNude() then return end
      local color, pattern = "black", ".|.|spade,club"
      if data.card.trueName == "jink" then
        color, pattern = "red", ".|.|heart,diamond"
      end
      if not table.find(player:getCardIds("he"), function(id)
            return Fk:getCardById(id):getColorString() == color
          end) then
        return
      end
      local card = room:askForCard(player, 1, 1, true, sk__chuyuan.name, false, pattern, "#sk__chuyuan-card:::" .. color)
      player:addToPile("sk__godcaopi_chu", card, true, sk__chuyuan.name, player.id)
    else
      data.n = data.n + #table.filter(player:getPile("sk__godcaopi_chu"), function(id)
        return Fk:getCardById(id).color == Card.Black
      end)
    end
  end,
})
sk__chuyuan:addEffect(fk.DrawNCards, {
  name = "sk__chuyuan",
  anim_type = "drawcard",
  derived_piles = "sk__godcaopi_chu",
  events = { fk.CardUseFinished, fk.DrawNCards },
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__chuyuan.name) and player:usedSkillTimes("sk__dengji", Player.HistoryGame) == 0 then
      if event == fk.CardUseFinished then
        return data.card.trueName == "slash" or data.card.trueName == "jink"
      else
        return target == player and table.find(player:getPile("sk__godcaopi_chu"), function(id)
          return Fk:getCardById(id).color == Card.Black
        end)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.CardUseFinished then
      return player.room:askForSkillInvoke(player, sk__chuyuan.name)
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.n = data.n + #table.filter(player:getPile("sk__godcaopi_chu"), function(id)
      return Fk:getCardById(id).color == Card.Black
    end)
  end,
})

sk__chuyuan:addEffect("maxcards", {
  name = "#sk__chuyuan_maxcards",
  --sk__chuyuan,
  correct_func = function(self, player)
    if player:hasSkill(sk__chuyuan.name) then
      return #table.filter(player:getPile("sk__godcaopi_chu"), function(id)
        return Fk:getCardById(id).color == Card.Red
      end)
    else
      return 0
    end
  end,
})

return sk__chuyuan
